package com.basker.pisces.context;

import org.springframework.util.Assert;

import com.basker.pisces.cache.IRedisCacheKey;
import com.basker.pisces.utils.GUIDUtils;

/**
 * 令牌对象，用于获取对应登录上下文
 *
 * @author hangwen
 * @see LoginContext
 * @see ILoginContextManager
 */
public class Token implements IRedisCacheKey {
    private String tokenId;

    public Token() {
    }

    public Token(String tokenId) {
        Assert.hasText(tokenId, "parameter 'tokenId' is required");
        this.tokenId = tokenId;
    }

    /**
     * 创建令牌，id为UUID
     */
    public static Token newToken() {
        return new Token(GUIDUtils.generateGUID());
    }

    @Override
    public String asKey() {
        return this.tokenId;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }

        if (this == obj) {
            return true;
        }

        if (!obj.getClass().equals(this.getClass())) {
            return false;
        }

        return this.tokenId.equals(((Token) obj).getTokenId());
    }

    /**
     * 令牌id
     *
     * @return
     */
    public String getTokenId() {
        return tokenId;
    }

    public void setTokenId(String tokenId) {
        Assert.hasText(tokenId, "parameter 'tokenId' is required");
        this.tokenId = tokenId;
    }

    @Override
    public int hashCode() {
        return tokenId.hashCode();
    }

}
